// components/order-card/order-card.js
import dayjs from 'dayjs'
import {
  cancelOrder,
  deleteOrder
} from "../../service/order"
import {
  orderStatus
} from "../../constant/OrderConstant"
Component({
  options: {
    styleIsolation: 'shared',
  },
  /**
   * 组件的属性列表
   */
  properties: {
    orderInfo: {
      type: Object,
      value: {},
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    expiredDate: "",
    orderStatus: orderStatus
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 点击导航到订单详情页
     */
    onTapOrderDetail() {
      const orderId = this.data.orderInfo.orderId
      wx.navigateTo({
        url: `/pages/OrderDetail/OrderDetail?orderId=${orderId}`,
      })
    },
    /**
     * 点击复制按钮
     */
    onTapCopyButton() {
      wx.setClipboardData({
        data: this.data.orderInfo.orderNumber,
        success(res) {
          // 无视此次的警告
          wx.hideLoading();
          wx.showToast({
            title: '复制成功',
            icon: 'none',
            duration: 2000
          })
        }
      })
    },
    /**
     * 点击取消订单按钮
     */
    onTapCancelButton() {
      const self = this
      wx.showModal({
        title: '提示',
        content: '确认取消订单？',
        complete: async (res) => {
          if (res.cancel) {}
          if (res.confirm) {
            wx.showLoading({
              title: '加载中',
              mask: true
            })
            await cancelOrder(self.data.orderInfo.orderId)
            wx.hideLoading()
            self.setData({
              "orderInfo.status": orderStatus.CANCELLED
            })
          }
        }
      })
    },
    /**
     * 点击删除按钮
     */
    onTapDeleteButton() {
      const self = this
      wx.showModal({
        title: '提示',
        content: '确认删除吗😭？',
        complete: async (res) => {
          if (res.cancel) {}
          if (res.confirm) {
            wx.showLoading({
              title: '加载中',
            })
            await deleteOrder(self.data.orderInfo.orderId)
            self.triggerEvent("deleteEvent", {
              orderId: self.data.orderInfo.orderId
            })
            wx.hideLoading()
          }
        }
      })
    },
    /**
     * 点击立即支付按钮
     */
    onTapBuyNow() {

    },
    /**
     * 再次购买
     */
    onTapRepeatPurchase() {

    },
    /**
     * 点击加入购物车
     */
    onTapAddToShippingCart() {

    },
    /**
     * 点击修改地址
     */
    onTapModifyDeliveryAddress() {

    },
    /**
     * 点击催发货
     */
    onTapUrgeForShipment() {

    },
    /**
     * 点击申请开票
     */
    onTapApplyForInvoice() {

    },
    /**
     * 点击查看物流
     */
    onTapCheckShipment() {

    },
    /**
     * 点击确认收货
     */
    onTapConfirmReceipt() {

    },
    /**
     * 点击立即评价
     */
    onTapRateNow() {

    }
  },
  lifetimes: {
    attached() {
      console.log(this.data);
      // 如果是未付款状态，计算订单自动取消的时间
      if (this.data.orderInfo.status === orderStatus.PENDING_PAYMENT) {
        const futureDate = dayjs(this.data.orderInfo.createTime).add(72, 'hour');
        this.setData({
          expiredDate: futureDate.format('MM.DD HH:mm')
        })
      }
    }
  }
})